// pages/uploadCerface/uploadCerface.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showModal: false,
        cer_face: '',
        QRCodeType: '',
        name: '',
        cer_code: '',
        enterprise_professional_facilitator_id:''
    },
    delImg() {
        this.setData({
            cer_face: ''
        })
    },
    changeModalStaus() {
        this.setData({
            showModal: !this.data.showModal
        })
    },
    identity() {
        this.changeModalStaus();
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success: res => {
                const tempFilePaths = res.tempFilePaths;
                this.upimg(tempFilePaths[0])
            }
        })
    },

    upimg(tempFilePath) {
        wx.uploadFile({
            url: wx.$config.upload,
            filePath: tempFilePath,
            name: 'file',
            formData: {
                token: wx.getStorageSync('token')
            },
            success: res => {
                let data = typeof res.data == "string" ? JSON.parse(res.data) : null;
                console.log(data)
                this.setData({
                    cer_face: data.data.resource_href
                })
            }
        })
    },
    getUserInfo() {
        wx.showLoading({
            title: '加载中...',
            mask: true,
            icon: "none"
        })
        let getUserInfo = wx.$api.getUserInfo;
        if (this.data.QRCodeType == 'naturalPerson') {
          getUserInfo = wx.$api.getUserInfoNatural;
        }
        getUserInfo().then(res => {
          wx.hideLoading()
          if (res.data.code == 200) {
            let {
              cer_face, // 半身照
              name, // 姓名
              cer_code, // 身份证号码
            } = res.data.data.info || {};
            this.setData({
              cer_face, // 半身照
              name, // 姓名
              cer_code, // 身份证号码
            })
          } else {
            wx.$Toast(res.data.msg);
          }
        }).catch(ret => {
          wx.hideLoading()
          wx.$Toast(ret.msg || '服务器繁忙，请稍候重试');
        })
      },
    submit() {
        if (!this.data.cer_face) {
            wx.$Toast("请上传照片");
            return;
        }
        wx.showLoading({
            title: '加载中...',
            mask: true,
            icon: "none"
        })
        if (this.data.QRCodeType == 'naturalPerson') {
            wx.$api.saveCerFace({
                cer_face: this.data.cer_face
            }).then(res => {
                wx.hideLoading();
                if (res.data.code == '200') {
                    wx.$Toast('提交成功');
                   setTimeout(()=>{
                    wx.switchTab({
                        url: '/pages/index/index',
                      })
                   },500)
                } else {
                    wx.$Toast(res.data.msg);
                }
            })
        } else {
            wx.$api.saveProfessionalFace({
                cer_face: this.data.cer_face
            }).then(res => {
                wx.hideLoading();
                if (res.data.code == '200') {
                    wx.$Toast('提交成功')
                   setTimeout(()=>{
                    wx.switchTab({
                        url: '/pages/index/index',
                      })
                   },500)
                } else {
                    wx.$Toast(res.data.msg);
                }
            })
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let QRCodeType = wx.getStorageSync('QRCodeType');
        this.setData({
            QRCodeType: QRCodeType || ""
        })
        this.getUserInfo();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})